#!/usr/bin/python
# -*- coding: utf-8 -*-



import tushare as ts
import pandas as pd
import json


df = ts.get_today_all()
# df_raw_data为只选取要处理的列，changepercent:涨跌幅，trade:现价，high：最高价，low:最低价，amount:成交额
df_raw_data = df[['name', 'trade', 'changepercent', 'high', 'low', 'amount']]

# 停牌家数；成交金额为0，最高价为0，（停牌：suspension）
data = {'suspension': [len(df_raw_data[(df_raw_data.amount == 0) & (df_raw_data.high == 0.00)])]}
# 定义最终传递的pandas为df_final_data
df_final_data = pd.DataFrame(data)

# 筛选ST票的方法
# df_final_data['ST'] = len(df_raw_data[df_raw_data.name.str.contians('s') == True])
# ST跌停板：名称包含S，跌幅大于-4.65，现价等于最低价
df_final_data['STdt'] = len(df_raw_data[(df_raw_data.name.str.contains('s') == True) & (df_raw_data.changepercent < -4.65) & (df_raw_data.trade == df_raw_data.low)])
# 非ST跌停家数：跌幅大于 -9.65（1.34*1.1），现价等于最低价
df_final_data['nSTdt'] = len(df_raw_data[(df_raw_data.changepercent < -9.65) & (df_raw_data.trade == df_raw_data.low)])

# 循环出涨幅1到9的，放入dataFrame
for i in range(-9, 10):
    # 小于-9，但没跌停
    if i == -9:
        df_final_data[i] = len(df_raw_data[(df_raw_data.changepercent <= -9) & (df_raw_data.changepercent > i - 1) & (
df_raw_data.trade > df_raw_data.low)])
# -1到-8的数据
    if i <= -1 and i > -9:
        df_final_data[i] = len(df_raw_data[(df_raw_data.changepercent <= i) & (df_raw_data.changepercent > i - 1)])
# 涨跌在1到-1之间的数据
    if i == 0:
        df_final_data['0'] = len(df_raw_data[(df_raw_data.changepercent > -1) & (df_raw_data.changepercent < 1) & (df_raw_data.amount > 1)])
# 1到8的数据
    if i >= 1 and i < 9:
        df_final_data[i] = len(df_raw_data[(df_raw_data.changepercent >= i) & (df_raw_data.changepercent < i + 1)])
# 大于9,但没涨停
    if i == 9:
        df_final_data[i] = len(df_raw_data[(df_raw_data.changepercent >= i) & (df_raw_data.changepercent < i + 1) & (
    df_raw_data.high > df_raw_data.trade)])

    #非ST涨停家数：涨幅大于9.65（1.34*1.1），现价等于最高价
df_final_data['nSTzt'] = len(df_raw_data[(df_raw_data.changepercent > 9.65)&(df_raw_data.trade == df_raw_data.high)])
#ST涨停板：名称包含S，涨幅大于4.65，现价等于最高价
df_final_data['STzt'] = len(df_raw_data[(df_raw_data.name.str.contains('S') == True)&(df_raw_data.changepercent >4.65)&(df_raw_data.trade == df_raw_data.high)])

print(df_final_data.to_json())